// request.js 使用示例

import { get, post, put, del, upload } from './request.js'

// 使用示例：在页面中如何调用

// 1. GET请求示例
export const getServiceList = async (params) => {
  try {
    const response = await get('/mobile/service/search', params);
    return response.data;
  } catch (error) {
    console.error('获取服务列表失败:', error);
    throw error;
  }
};

// 2. POST请求示例
export const createOrder = async (orderData) => {
  try {
    const response = await post('/mobile/order/create', orderData);
    return response.data;
  } catch (error) {
    console.error('创建订单失败:', error);
    throw error;
  }
};

// 3. PUT请求示例（更新数据）
export const updateUserInfo = async (userData) => {
  try {
    const response = await put('/mobile/user/update', userData);
    return response.data;
  } catch (error) {
    console.error('更新用户信息失败:', error);
    throw error;
  }
};

// 4. DELETE请求示例
export const deleteOrder = async (orderId) => {
  try {
    const response = await del(`/mobile/order/${orderId}`);
    return response.data;
  } catch (error) {
    console.error('删除订单失败:', error);
    throw error;
  }
};

// 5. 文件上传示例
export const uploadAvatar = async (filePath) => {
  try {
    const response = await upload('/mobile/file/upload', filePath, {
      type: 'avatar'
    }, { name: 'avatar' });
    return response.data;
  } catch (error) {
    console.error('上传头像失败:', error);
    throw error;
  }
};

// 在页面中使用的完整示例：

/*
// 在页面的script部分导入
import { get, post } from '@/utils/request.js'

export default {
  data() {
    return {
      serviceList: [],
      loading: false
    }
  },
  
  async onLoad() {
    await this.loadServices();
  },
  
  methods: {
    // 加载服务列表
    async loadServices() {
      try {
        this.loading = true;
        
        // 使用GET方法获取数据，token会自动携带
        const response = await get('/mobile/service/search', {
          name: '',
          typeId: null,
          merchantId: null
        });
        
        this.serviceList = response.data;
        
      } catch (error) {
        // 错误处理已在request.js中统一处理
        console.log('加载失败');
      } finally {
        this.loading = false;
      }
    },
    
    // 创建订单
    async createOrder(serviceId) {
      try {
        // 使用POST方法提交数据
        const response = await post('/mobile/order/create', {
          serviceId: serviceId,
          quantity: 1,
          remarks: '备注信息'
        });
        
        uni.showToast({
          title: '订单创建成功',
          icon: 'success'
        });
        
        // 跳转到订单详情页
        uni.navigateTo({
          url: `/pages/order/detail?id=${response.data.id}`
        });
        
      } catch (error) {
        // 错误已被统一处理，这里可以做额外的处理
        console.log('创建订单失败');
      }
    },
    
    // 不显示loading的请求
    async quickCheck() {
      try {
        const response = await get('/mobile/user/info', {}, { loading: false });
        console.log('用户信息:', response.data);
      } catch (error) {
        console.log('获取用户信息失败');
      }
    }
  }
}
*/

